package main

import (
	"context"
	"fmt"
)

var (
	indexName = "user_index"
	typeName  = "user"
)

//新建node索引
//根据索引名建立node索引
func newNodeIndex() error {
	mapping := `
	{ "mappings":
 		{ "user":
    		{ "properties":
        		{
            	"id":{ "type": "text" },
           	 	"name":{ "type": "text" },
            	"age":{"type":"integer"},
				"des":{"type":"text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word"}
       		 	}
			}	
		}
 	}`
	//创建索引为node
	_, err := client.CreateIndex(indexName).BodyString(mapping).Do(context.Background())
	if err != nil {
		fmt.Printf("创建索引%s出错 %v\n", indexName, err)
		return err
	}
	fmt.Printf("创建node索引 索引名为%s", indexName)
	return nil
}

//判断索引存在否
func existIndex() {
	exist, err := client.IndexExists(indexName).Do(context.Background())
	if err != nil {
		fmt.Printf("查询索引%s存在出错 %v\n", indexName, err)
		return
	}
	if exist {
		fmt.Printf("索引%s存在\n", indexName)
	} else {
		fmt.Printf("索引%s不存在\n", indexName)
	}
}

//删除某个索引
func deleteIndex() {
	res, err := client.DeleteIndex(indexName).Do(context.Background())
	if err != nil {
		fmt.Printf("删除索引%s存在出错 %v\n", indexName, err)
		return
	}
	fmt.Println(res)
}
